/*
 * ProgressDialog2.java
 *
 * Created on May 18, 2005, 10:41 PM
 */

package ika.gui;

import javax.swing.*;

/**
 *
 * @author  jenny
 */
public class ImageReaderProgressDialog extends javax.swing.JDialog
        implements javax.imageio.event.IIOReadProgressListener{
    
    private boolean cancel = false;
    
    /**
     * Creates new form ImageReaderProgressDialog
     */
    public ImageReaderProgressDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        outterPanel = new javax.swing.JPanel();
        cancelButton = new javax.swing.JButton();
        progressBar = new javax.swing.JProgressBar();
        label = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 35, 16));

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Reading Image");
        setModal(true);
        setName("progressDialog");
        setResizable(false);
        outterPanel.setLayout(new java.awt.GridBagLayout());

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        outterPanel.add(cancelButton, gridBagConstraints);

        progressBar.setPreferredSize(new java.awt.Dimension(250, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 20);
        outterPanel.add(progressBar, gridBagConstraints);

        label.setText("Reading the image. Please wait...");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 0);
        outterPanel.add(label, gridBagConstraints);

        getContentPane().add(outterPanel);

        pack();
    }
    // </editor-fold>//GEN-END:initComponents
    
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.cancel = true;
    }//GEN-LAST:event_cancelButtonActionPerformed
    
    public void disableCancel() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                cancelButton.setEnabled(false);
                cancelButton.setFocusable(false);
            }
        });
    }
    
    public void finish() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                dispose();
            }
        });
    }
    
    public void showProgressDialog() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // center progress dialog in primary screen
                java.awt.Dimension screen = getToolkit().getScreenSize();
                java.awt.Dimension dialog = getSize();
                setLocation( (screen.width - dialog.width) / 2,
                        (screen.height - dialog.height) / 2 );
                
                setVisible(true);
                if (progressBar != null)
                    progressBar.setEnabled(true);
            }
        });
    }
    
    public boolean isCanceled() {
        return cancel;
    }
    
    public void imageStarted(javax.imageio.ImageReader imageReader, int param) {
        showProgressDialog();
        this.cancel = false;
    }
    
    public void imageComplete(javax.imageio.ImageReader source) {
    }
    
    public void imageProgress(javax.imageio.ImageReader imageReader,
            final float percentage) {
        if (this.cancel) {
            imageReader.abort();
            this.cancel = false;
        } else {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (progressBar != null)
                        progressBar.setValue((int)percentage);
                }
            });
        }
    }
    
    public void readAborted(javax.imageio.ImageReader source) {
        this.cancel = true;
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                dispose();
            }
        });
    }
    
    public void sequenceComplete(javax.imageio.ImageReader source) {
    }
    
    public void sequenceStarted(javax.imageio.ImageReader imageReader, int param) {
    }
    
    public void thumbnailComplete(javax.imageio.ImageReader source) {
    }
    
    public void thumbnailProgress(javax.imageio.ImageReader imageReader, float param) {
    }
    
    public void thumbnailStarted(javax.imageio.ImageReader imageReader, int param, int param2) {
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel label;
    private javax.swing.JPanel outterPanel;
    private javax.swing.JProgressBar progressBar;
    // End of variables declaration//GEN-END:variables
    
}
